import { defineConfig, loadEnv } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
import vue from '@vitejs/plugin-vue';
import * as path from 'path';
// import progress from 'vite-plugin-progress';./

export default defineConfig(({ command, mode, ssrBuild }) => {
  return {
    plugins: [
      vue(),
      // progress(),
      visualizer({
        open: true, //注意这里要设置为true，否则无效
        filename: 'stats.html', //分析图生成的文件名
        gzipSize: true, // 收集 gzip 大小并将其显示
        brotliSize: true, // 收集 brotli 大小并将其显示
      }),
    ],
    base: '/',
    root: process.cwd(),
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@img': path.resolve(__dirname, 'src/assets'),
        '@com': path.resolve(__dirname, 'src/components'),
        '@utils': path.resolve(__dirname, 'src/utils'),
        '@api': path.resolve(__dirname, 'src/api'),
        '@views': path.resolve(__dirname, 'src/views'),
        '@router': path.resolve(__dirname, 'src/router'),
        '@pinia': path.resolve(__dirname, 'src/pinia'),
        '@config': path.resolve(__dirname, 'src/config'),
      },
    },
    define: {
      'process.env': process.env,
    },
    server: {
      host: true,
      port: 8080,
      proxy: {
        '/api': {
          target: loadEnv(mode, process.cwd()).VITE_APP_API_BASE_URL,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/api/, ''),
        },
        '/bgapi': {
          target: loadEnv(mode, process.cwd()).VITE_APP_API_BASE_URL_BG,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/bgapi/, ''),
        },
        '/mesapi': {
          target: loadEnv(mode, process.cwd()).VITE_APP_API_BASE_URL_MES,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/mesapi/, ''),
        },
        '/print': {
          target: loadEnv(mode, process.cwd()).VITE_APP_API_BASE_URL_PRINT,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/print/, ''),
        },
        '/erpapi': {
          target: loadEnv(mode, process.cwd()).VITE_APP_API_BASE_URL_ERP,
          changeOrigin: true,
          rewrite: (path: any) => path.replace(/^\/erpapi/, ''),
        },
      },
    },
  };
});
